<?php

class RpcModel
{
    private $diaryListCacheModel;
    private $diaryListModel;
    private $clientLogPrefix = 'CL/';

    function __construct()
    {
		$this->diaryListCacheModel    =    new DiaryListCacheModel();
		$this->diaryListModel         =    new DiaryListModel();
    }
    
    /**
     * 热门列表
     * 
     * @param array $info
     * @return NULL|array $list
     */
    public function getChosenDiary($info)/*{{{*/
    {    	
        $hotids = $this->getHotDiary($info);
    	
    	if(empty($hotids)){
    		return null;
    	}
    	
    	foreach ($hotids as $hotid){
    		$value = array();
    		$diary = $this->diaryListModel->getDiaryInfoByBodyId($hotid);
    		if (empty($diary)){
    		    continue;
    		}
    		$value['id'] = strval($diary['id']);
    		$value['eUid'] = strval($diary['uid']);
    		$value['eNickname'] = strval($diary['nickname']);
    		$value['avatar']    = $diary['avatar'];
    		$value['bodyId'] = strval($diary['bodyId']);
    		$value['type'] = strval($diary['type']);
    		$value['title'] = strval($diary['title']);
    		$value['abbreviation'] = strval($diary['abbreviation']);
    		$value['time'] = strval($diary['time']);
    		$value['status'] = strval($diary['status']);
    		$value['periodType'] = empty($diary['periodType']) ? '' : strval($diary['periodType']) ;
    		$value['viewTimes'] = strval($diary['viewTimes']);
    		$value['commentTimes'] = strval($diary['commentTimes']);
    		$value['likeTimes'] = strval($diary['likeTimes']);
    		$value['attachmentPath'] = strval($diary['attachmentPath']);
    		$value['chosen'] = "0";
    		$value['commentPeople'] = strval($diary['commentPeople']);
    		$value['grossScore'] = empty($diary['grossScore']) ? '0' : strval($diary['grossScore']) ;
    		$value['rUid']     =    $diary['rUid'];
    		$value['rBodyId']  =    $diary['rBodyId'];
    		$list[] = $value;
    	}
    	
    	return  $list;
    }/*}}}*/	
    
    
    /**
     * 
     */
    public function getChosenDiaryByCategory($info, $category){
        
        $diaryids = $this->getHotDiaryByCategory($info, $category);
    	
    	if(empty($diaryids)){
    		return null;
    	}
    	
    	foreach ($diaryids as $bodyId){    
    		$value = array();
    		$diary = $this->diaryListModel->getDiaryInfoByBodyId($bodyId);
    		
    		if (empty($diary)){
    		    continue;
    		}
    		$value['id'] = strval($diary['id']);
    		$value['eUid'] = strval($diary['uid']);
    		$value['eNickname'] = strval($diary['nickname']);
    		$value['bodyId'] = strval($diary['bodyId']);
    		$value['type'] = strval($diary['type']);
    		$value['title'] = strval($diary['title']);
    		$value['abbreviation'] = strval($diary['abbreviation']);
    		$value['time'] = strval($diary['time']);
    		$value['status'] = strval($diary['status']);
    		$value['periodType'] = empty($diary['periodType']) ? '' : strval($diary['periodType']) ;
    		$value['viewTimes'] = strval($diary['viewTimes']);
    		$value['commentTimes'] = strval($diary['commentTimes']);
    		$value['likeTimes'] = strval($diary['likeTimes']);
    		$value['attachmentPath'] = strval($diary['attachmentPath']);
    		$value['chosen'] = "0";
    		$value['commentPeople'] = strval($diary['commentPeople']);
    		$value['grossScore'] = empty($diary['grossScore']) ? '0' : strval($diary['grossScore']) ;
    	    	$value['rUid']     =    $diary['rUid'];
    	   	$value['rBodyId']  =    $diary['rBodyId'];
    		$list[] = $value;
    	}
    	return $list;
    }
    
    public function getHotDiaryByCategory($info, $category){
        $start      =   isset($info['start'])  ?    $info['start'] :   0;
        $length     =   isset($info['length']) ?    $info['length']:   AC::DIARY_LIST_DEFAULT_LEN;
        $end = $start + $length - 1;
    
        $list = array();
         
        $diaryids = $this->diaryListCacheModel->getHotDiaryListByCategory($category, $start, $end);
        if(empty($diaryids)){
            return null;
        }
       
        return $diaryids;
    }
    
    public function getHotDiary($info)/*{{{*/
    {
        $start      =   isset($info['start'])  ?    $info['start'] :   0;
        $length     =   isset($info['length']) ?    $info['length']:   AC::DIARY_LIST_DEFAULT_LEN;
        $end = $start + $length - 1;
         
        $list = array();
        $hotids  = $this->diaryListCacheModel->getHotDiaryListAll($start, $end);
        if(empty($hotids)){
            return null;
        }
        return $hotids;
    }
    
    public function getHotDiaries ($id, $length) {
        
        $result  = $this->diaryListCacheModel->getHotDiaries($id, $length);
        if(empty($result)){
            return array();
        }
        return $result;
    }
    
    public function getHotRecommendDiary ($id, $unionKeys, $length) {
        $result = $this->diaryListCacheModel->getHotRecommendDiary($id, $unionKeys, $length);
        if (empty($result)) {
            return array();
        }
        
        return $result;
    }

    public function addClientlog($data){
        $new = [
            'uid' => isset($data['uid']) ? intval($data['uid']) : 0,
            'type' => isset($data['type']) ? intval($data['type']) : 0,
            'status' => 0,
            'rate' => 0,
            'client' => isset($data['client']) ? trim($data['client']) : '',
            'logpath' => isset($data['logpath']) ? trim($data['logpath']) : '',
            'create_at' => SERVER_TIME,
            'extra' => ''
        ];
        $key = $this->clientLogPrefix. 0;
        $result = Storage::addData($key, $new);
        return $result;
    }
}
